<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Menu {

    protected $CI;  

    function Menu() {
        $this->CI = & get_instance();
    }

    public function getMenu($datos = array()){
        if(array_key_exists('men_id', $datos)){
            $this->CI->db->where('men_id', $datos['men_id']);
        }else{
            $this->CI->db->where('men_parent', null);
        }
        $this->CI->db->where('bmr_fk_rol', $this->CI->session->userdata('usu_fk_rol'));
        $this->CI->db->join('base_menu_rol', 'base_menu_rol.bmr_fk_base_menu = base_menu.men_id');
        $this->CI->db->order_by('men_orden', 'asc');
        $query = $this->CI->db->get('base_menu');
        $array = array();
        $cont = 0;
        foreach($query->result_array() as $row){
            $array[$cont] = $row;
            $this->CI->db->where('men_parent', $row['men_id']);
            $this->CI->db->order_by('men_orden', 'asc');
            $array[$cont]['d_hijos'] = $this->CI->db->get('base_menu')->result_array();
            $cont++;
        }
        return $array;
    }
    
    public function setMenu(){
        $men_id = $_POST['men_id'];
        $data = array(
            'men_slug' => $this->CI->input->post('men_slug'),
            'men_desc' => $this->CI->input->post('men_desc'),
            'men_parent' => $this->CI->input->post('men_parent') != '' ? $this->CI->input->post('men_parent') : null,
            'men_seccion' => $this->CI->input->post('men_seccion'),
            'men_orden' => $this->CI->input->post('men_orden'),
            'men_enlace' => $this->CI->input->post('men_enlace'),
            'men_icono' => $this->CI->input->post('men_icono') != '' ? $this->CI->input->post('men_icono') : null,
            'men_isenlace' => $this->CI->input->post('men_isenlace')
        );
        if($men_id != null){
            $this->CI->db->where('men_id', $this->CI->input->post('men_id'));
            $this->CI->db->update('base_menu', $data);
        }else{
            $this->CI->db->insert('base_menu', $data);
            $men_id = $this->CI->db->insert_id();
        }
        $this->CI->db->delete('base_menu_rol', array('bmr_fk_base_menu' => $men_id));
        foreach($_POST['men_roles'] as $rol){
            $this->CI->db->insert('base_menu_rol', array('bmr_fk_base_menu' => $men_id, 'bmr_fk_rol' => $rol));
        }
        return $this->CI->db->_error_message();
    }
    
    public function getRolesMenu($menu = null){
        $this->CI->db->where('bmr_fk_base_menu', $menu);
        return $this->CI->db->get('base_menu_rol')->result_array();
    }

    public function getDatoActual($dato){
        foreach($this->CI->session->userdata('menu') as $menu){
            if(trim("panel/".$menu['men_enlace'], '/') == trim($this->CI->uri->uri_string(), '/')){
                return $menu[$dato];
            }
            foreach($menu['d_hijos'] as $submenu){
                if(trim("panel/".$submenu['men_enlace'], '/') == trim(get_uri_string(), '/')){
                    return $submenu[$dato];
                }
            }
        }
    }

    public function getRutaActual(){
        $ruta = array();
        foreach($this->CI->session->userdata('menu') as $menu){
            foreach($menu['d_hijos'] as $submenu){
                if(trim("panel/".$submenu['men_enlace'], '/') == trim(get_uri_string(), '/')){
                    $ruta[] = $menu;
                    $ruta[] = $submenu;
                    return $ruta;
                }
            }
        }
        return array();
    }
}

?>